home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-02 / oop_tp55.zip / LIST2_3.PAS < prev    next >
Pascal/Delphi Source File  |  1990-03-08  |  7KB  |  262 lines

  1. program Listing2_3;
  2.  
  3. uses Crt; { for KeyPressed }
  4.  
  5. const
  6.  
  7. ON  = true;
  8. OFF = false;
  9. MinR = 0;       { Minimum number of counts in an analog input }
  10. MaxR = 4095;    { Maximum number of counts in an analog input }
  11.  
  12. var
  13.     f : text;  {predefined file type}
  14.  
  15.  
  16. type
  17.  
  18. Resolution = 0..4095;  { Range for analog input }
  19. String12 = string[12];
  20.  
  21. Tag = object
  22.       TagNumber : String12;
  23.       procedure Init( ATag : String12 );
  24.       end;
  25.  
  26. Digital = object(Tag)
  27.           Status : boolean;
  28.           procedure Init( ATag : String12; AStatus : boolean );
  29.           procedure PutStatus( AStatus : boolean );
  30.           function GetStatus : boolean;
  31.           end;
  32.  
  33. Analog = object(Tag)
  34.          Value : Resolution;
  35.          ZeroVal : real;
  36.          MaxVal : real;
  37.          Slope  : real;
  38.          procedure Init( ATag : String12; AValue : Resolution;
  39.                          Min, Max : real );
  40.          procedure PutValue( AValue : real );
  41.          function GetValue : real;
  42.          end;
  43.  
  44. DOutput = object(Digital)
  45.           end;
  46.  
  47. Pump = object(DOutput)
  48.        FlowRate : real;
  49.        procedure Init( ATag : String12; AStatus : boolean; AFlow : real );
  50.        function Flow : real;
  51.        end;
  52.  
  53.  
  54. DInput = object(Digital)
  55.             Setpoint  : real;
  56.             Reading   : real;
  57.             procedure PutSetpoint( NewSetpoint : real );
  58.             end;
  59.  
  60. HiSwitch = object(DInput)
  61.            procedure Init( ATag : string;
  62.                          ASetpoint : real;
  63.                          AReading : real);
  64.            procedure PutReading( NewReading : real );
  65.            end;
  66.  
  67. LoSwitch = object(DInput)
  68.            procedure Init( ATag : string;
  69.                          ASetpoint : real;
  70.                          AReading : real);
  71.            procedure PutReading( NewReading : real );
  72.            end;
  73.  
  74. procedure Tag.Init( ATag : String12 );
  75. begin
  76.      TagNumber := ATag;
  77. end;
  78.  
  79. procedure Digital.Init( ATag : String12; AStatus : boolean );
  80. begin
  81.      Tag.Init( ATag );
  82.      Status := AStatus;
  83. end;
  84. procedure Digital.PutStatus( AStatus : boolean );
  85. begin
  86.      Status := AStatus;
  87. end;
  88. function Digital.GetStatus : boolean;
  89. begin
  90.      if Status = on then
  91.         writeln( f, TagNumber, ' is ON.' )
  92.      else
  93.         writeln( f, TagNumber, ' is OFF.' );
  94.      GetStatus := Status;
  95. end;
  96.  
  97. procedure Pump.Init( ATag : String12; AStatus : boolean; AFlow : real );
  98. begin
  99.      Digital.Init( ATag, AStatus );
  100.      FlowRate := AFlow;
  101. end;
  102.  
  103. function Pump.Flow : real;
  104. begin
  105.      Flow := FlowRate;
  106. end;
  107.  
  108. procedure Analog.Init( ATag : String12;
  109.                        AValue : Resolution;
  110.                        Min, Max : real );
  111. begin
  112.      Tag.Init( ATag );
  113.      Value := AValue;
  114.      MaxVal := Max;
  115.      ZeroVal := Min;
  116.      Slope := (Max-Min)/(MaxR-MinR);
  117. end;
  118.  
  119. procedure Analog.PutValue( AValue : real );
  120. begin
  121.      if AValue > MaxVal then
  122.         AVAlue := MaxVal
  123.      else
  124.         if AValue < ZeroVal then
  125.            AValue := ZeroVal;
  126.      Value := Round((AValue - ZeroVal)/Slope);
  127. end;
  128.  
  129. function Analog.GetValue : real;
  130. begin
  131.      GetValue := Slope*Value + ZeroVal;
  132. end;
  133.  
  134. procedure DInput.PutSetpoint( NewSetpoint : real );
  135. begin
  136.      Setpoint := NewSetpoint;
  137. end;
  138.  
  139. procedure LoSwitch.Init( ATag : string;
  140.                          ASetpoint : real;
  141.                          AReading : real);
  142. begin
  143.      Tag.Init( ATag );
  144.      DInput.PutSetpoint( ASetpoint );
  145.      PutReading( AReading );
  146. end;
  147.  
  148. procedure LoSwitch.PutReading( NewReading : real );
  149. begin
  150.      Reading := NewReading;
  151.      if Reading <= Setpoint then
  152.         Status := true
  153.      else
  154.         Status := false;
  155. end;
  156.  
  157. procedure HiSwitch.Init( ATag : string;
  158.                          ASetpoint : real;
  159.                          AReading : real);
  160. begin
  161.      Tag.Init( ATag );
  162.      DInput.PutSetpoint( ASetpoint );
  163.      PutReading( AReading );
  164. end;
  165.  
  166. procedure HiSwitch.PutReading( NewReading : real );
  167. begin
  168.      Reading := NewReading;
  169.      if Reading >= Setpoint then
  170.         Status := true
  171.      else
  172.         Status := false;
  173. end;
  174.  
  175. { HtToPSI converts a height (of a column of water) into a pressure.
  176.   The math is pretty simple:  A column of water 2.31 feet high exerts
  177.   a force of one pound per square inch at the bottom. }
  178.  
  179. function HtToPSI( Height : real ) : real;
  180. begin
  181.      HtToPSI := Height/2.31;
  182. end;
  183.  
  184. { FlowToDeltaHt converts a flow into (or out of) our system into a
  185.   change in height in the tank.
  186.   The math is as follows:  Divide the flow, in gpm, by 7.48 gal/cu.ft.
  187.   to get the number of cubic feet pumped per minute.  Divide this
  188.   number by the volume of 1 vertical foot of the tank, which is
  189.   the radius squared times 'pi' (15*15*3.1416). }
  190.  
  191. function FlowToDeltaHt( Flow : real ) : real;
  192. begin
  193.      FlowToDeltaHt := Flow/(7.48 * 706) ;
  194. end;
  195.  
  196. var
  197.    LT100,
  198.    FT100  : Analog;
  199.    PSL100 : LoSwitch;
  200.    PSH100 : HiSwitch;
  201.    EY100  : Pump;
  202.    time   : integer;
  203.  
  204. begin
  205.  
  206.      { If no file name is passed to the program, then ParamStr(1)
  207.        will be the null string, which will cause Assign to output
  208.        to the standard output (the screen).  If a file name is
  209.        passed, the output will be saved in the file for later
  210.        reference. }
  211.  
  212.      Assign( f, ParamStr(1) );
  213.      Rewrite( f );
  214.  
  215.      LT100.Init( 'LT100', 512, 50, 250 );
  216.      FT100.Init( 'FT100', 2048, 0, 10000 );
  217.      PSL100.Init( 'PSL100', 60, HtToPSI(LT100.GetValue) );
  218.      PSH100.Init( 'PSH100', 75, HtToPSI(LT100.GetValue) );
  219.      EY100.Init( 'EY100', off, 8000);
  220.      time := 0;
  221.  
  222.      repeat
  223.  
  224.      Inc( time );  { increment the time }
  225.      { First, adjust the reading in LT-100 by reducing the level in
  226.        LT100.Value by an amount corresponding to the flow out of the
  227.        system.  We do this by fetching the value of FT100, converting
  228.        it to a height in the tank, and subtracting from the actual
  229.        height... }
  230.      LT100.PutValue( LT100.GetValue - FlowToDeltaHt(FT100.GetValue) );
  231.  
  232.      { We take the result and update the switches }
  233.      PSL100.PutReading( HtToPSI(LT100.GetValue) );
  234.      PSH100.PutReading( HtToPSI(LT100.GetValue) );
  235.  
  236.      {Let's publish the level in the tank...}
  237.      writeln( f, 'Level in tank is ', LT100.GetValue:2:2, ' feet at minute ', time );
  238.  
  239.      { If the pressure is too low, turn on the pump }
  240.      if PSL100.GetStatus = on then
  241.         EY100.PutStatus( ON );
  242.      { If the pressure is too high, turn off the pump }
  243.      if PSH100.GetStatus = on then
  244.         EY100.PutStatus( OFF );
  245.  
  246.      LT100.PutValue( LT100.GetValue + FlowToDeltaHt( EY100.Flow ));
  247.  
  248.      until KeyPressed;
  249.      Close ( f );
  250. end.
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.